#include "parser.h"

parser::parser()
{
}
//parsing a string into a vector of commands, each command follow by a string of arguements,
//a NULL string, then another command for piping purpose
//return 0 if the string is just a plain text string
//return 1 if successfully parse
int parser::parse(string in, vector<string> &command, vector<string> &path, vector<string> &data)
{
	int ci = 0;
	int csi = 0;
	ci=in.find_first_of("=");//| ");
	if(ci==string::npos)
	{
		ci=in.find_first_of("|");
		if(ci ==string::npos)
		{
                    ci = in.find("$?");
                     if(ci ==string::npos)
                     {
			ci=in.find_first_of(" ");
			if(ci ==string::npos)
			{
				command.push_back(in);
			}
                        else
                        {
			command.push_back(in.substr(0,ci));
			parse(in.substr(ci+1),command,path,data);
			}
                     }
                     else
                     {  // exit status
                         command.push_back(in.substr(0,ci+2)); //$?
                         command.push_back(in.substr(ci+2)); // stored exit index
                     }
		}
		else
		{
			parse(in.substr(0,ci),command,path,data);
			parse(in.substr(ci+1),command,path,data);
		}
	}

	if(in.substr(0,ci).compare("PATH")==0)
			{
				ci++;
				string temp = in.substr(ci);
				csi = temp.find_first_of(":");
				while(csi!=string::npos)
				{
					path.push_back(temp.substr(0,csi));
					temp.erase(0,csi+1);
					csi = temp.find_first_of(":");
				}
				path.push_back(temp);
			}
			else if(in.substr(0,ci).compare("DATA")==0)
			{
				ci++;
				string temp = in.substr(ci);
				csi = temp.find_first_of(":");
				while(csi!=string::npos)
				{
					data.push_back(temp.substr(0,csi));
					temp.erase(0,csi+1);
					csi = temp.find_first_of(":");
				}
				data.push_back(temp);
			}

		return 1;

}
parser::~parser()
{
}
